package com.avic.rong.search.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortBzDrawerDownTimeRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortBzDrawerStateRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortBzDrawerUpTimeRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsDrawerChuBanSheRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsDrawerDateRvAdapter;
import com.avic.rong.goodsSort.adapter.secondListAdapter.GoodsSortTsDrawerFenLeiRvAdapter;
import com.avic.rong.goodsSort.bean.GoodsSortDrawerBean;
import com.avic.rong.search.MessageEvent;
import com.avic.rong.search.adapter.SearchResultVpAdapter;
import com.avic.rong.search.api.SearchTsAttributeApi;
import com.avic.rong.search.bean.SearchTsAttributeBean;
import com.avic.rong.search.fragment.SearchAllFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchArticleFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchBiaoZhunFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchGoodsFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchPeiXunFragment;
import com.avic.rong.search.fragment.searchResultFragments.SearchWenDaFragment;
import com.avic.rong.utils.DateUtils;
import com.avic.rong.utils.EditUtils;
import com.example.minato.minastore.BaseResultEntity;
import com.example.minato.minastore.http.HttpManager;
import com.example.minato.minastore.listener.HttpOnNextListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SearchResaultActivity extends BaseActivity implements View.OnClickListener, GoodsSortBzDrawerUpTimeRvAdapter.GoodsSortBzDrawerUpTimeListener, GoodsSortBzDrawerStateRvAdapter.GoodsSortBzDrawerStateListener, GoodsSortBzDrawerDownTimeRvAdapter.GoodsSortBzDrawerDownTimeListener, GoodsSortTsDrawerFenLeiRvAdapter.GoodsSortTsDrawerFenLeiListener, GoodsSortTsDrawerChuBanSheRvAdapter.GoodsSortTsDrawerChuBanSheListener, GoodsSortTsDrawerDateRvAdapter.GoodsSortTsDrawerDateListener {

    @BindView(R.id.search_result_EditText)
    TextView search_result_EditText;
    @BindView(R.id.search_result_Cancel)
    TextView searchHomePageCancel;
    @BindView(R.id.search_result_Delete)
    ImageView searchHomePageDelete;
    @BindView(R.id.search_result_RelativeLayout)
    RelativeLayout searchResultRelativeLayout;
    @BindView(R.id.search_result_TabLayout)
    TabLayout searchTabLayout;
    @BindView(R.id.search_result_shaixuan_tv)
    TextView searchShaixuanTv;
    @BindView(R.id.search_result_ShaiXuan_Img)
    ImageView searchResultShaiXuanImg;
    @BindView(R.id.search_Result_zhanwei)
    TextView searchResultZhanwei;
    @BindView(R.id.search_result_Vp)
    ViewPager searchVp;
    @BindView(R.id.goods_sort_bz_drawer_zhankai)
    TextView goodsSortBzDrawerZhankai;
    @BindView(R.id.goods_sort_bz_drawer_topRv)
    RecyclerView goodsSortBzDrawerTopRv;
    @BindView(R.id.goods_sort_bz_drawer_fabu_start)
    TextView goodsSortBzDrawerFabuStart;
    @BindView(R.id.goods_sort_bz_drawer_fabu_end)
    TextView goodsSortBzDrawerFabuEnd;
    @BindView(R.id.goods_sort_bz_drawer_fabuRv)
    RecyclerView goodsSortBzDrawerFabuRv;
    @BindView(R.id.goods_sort_bz_drawer_shishi_start)
    TextView goodsSortBzDrawerShishiStart;
    @BindView(R.id.goods_sort_bz_drawer_shishi_end)
    TextView goodsSortBzDrawerShishiEnd;
    @BindView(R.id.goods_sort_bz_drawer_shishiRv)
    RecyclerView goodsSortBzDrawerShishiRv;
    @BindView(R.id.goods_sort_bz_drawer_qicaoEdit)
    EditText goodsSortBzDrawerQicaoEdit;
    @BindView(R.id.goods_sort_bz_drawer_danweiEdit)
    EditText goodsSortBzDrawerDanweiEdit;
    @BindView(R.id.goods_sort_bz_drawer_Reset)
    TextView goodsSortBzDrawerReset;
    @BindView(R.id.goods_sort_bz_drawer_Sure)
    TextView goodsSortBzDrawerSure;
    @BindView(R.id.goods_sort_bz_drawer)
    ConstraintLayout goodsSortBzDrawer;
    @BindView(R.id.lineaR_Ts)
    LinearLayout linearTs;
    @BindView(R.id.goods_sort_ts_drawer_fenlei_tv)
    TextView goodsSortTsDrawerFenleiTv;
    @BindView(R.id.goods_sort_ts_drawer_fenlei_rv)
    RecyclerView goodsSortTsDrawerFenleiRv;
    @BindView(R.id.goods_sort_ts_drawer_chubanshe_tv)
    TextView goodsSortTsDrawerChubansheTv;
    @BindView(R.id.goods_sort_ts_drawer_chubanshe_rv)
    RecyclerView goodsSortTsDrawerChubansheRv;
    @BindView(R.id.goods_sort_ts_drawer_author_edit)
    EditText goodsSortTsDrawerAuthorEdit;
    @BindView(R.id.goods_sort_ts_drawer_chubantime_start)
    TextView goodsSortTsDrawerChubantimeStart;
    @BindView(R.id.goods_sort_ts_drawer_chubantime_end)
    TextView goodsSortTsDrawerChubantimeEnd;
    @BindView(R.id.goods_sort_ts_drawer_chubantime_rv)
    RecyclerView goodsSortTsDrawerChubantimeRv;
    @BindView(R.id.goods_sort_ts_drawer_Reset)
    TextView goodsSortTsDrawerReset;
    @BindView(R.id.goods_sort_ts_drawer_Sure)
    TextView goodsSortTsDrawerSure;
    @BindView(R.id.goods_sort_ts_drawer)
    ConstraintLayout goodsSortTsDrawer;
    @BindView(R.id.lineaR_Goods)
    LinearLayout linearGoods;
    @BindView(R.id.search_Result_DrawerLayout)
    DrawerLayout searchResultDrawerLayout;

    private GoodsSortBzDrawerUpTimeRvAdapter drawerUptimeAdapter;
    private GoodsSortBzDrawerDownTimeRvAdapter drawerDowntimeAdapter;
    private GoodsSortBzDrawerStateRvAdapter drawerStateAdapter;

    private List<GoodsSortDrawerBean> timeList = new ArrayList<>();
    private List<GoodsSortDrawerBean> timeList1 = new ArrayList<>();
    private List<GoodsSortDrawerBean> stateList = new ArrayList<>();

    private GoodsSortTsDrawerFenLeiRvAdapter tsDrawerFenLeiRvAdapter;
    private GoodsSortTsDrawerChuBanSheRvAdapter tsDrawerChuBanSheRvAdapter;
    private GoodsSortTsDrawerDateRvAdapter tsDrawerDateRvAdapter;
    private List<SearchTsAttributeBean.BookCatBean> tsTsFenLeiDrawerList = new ArrayList<>();
    private ArrayList<GoodsSortDrawerBean> timeDrawerList = new ArrayList<>();
    private ArrayList<GoodsSortDrawerBean> chubansheDrawerList = new ArrayList();


    private List<String> titleList = new ArrayList<>();
    private List<Fragment> fragmentList = new ArrayList<>();
    private SearchResultVpAdapter vpAdapter;

    public static String nowSearchStr = "GJB";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_resault);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        //强制隐藏软键盘
//        EditUtils.hideSoftKeyboard(this);

        searchTabLayout.setupWithViewPager(searchVp);
        searchResultDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        nowSearchStr = getIntent().getStringExtra(SearchHomePageActivity.NOW_SEARCHSTR);
        search_result_EditText.setText(nowSearchStr);
        searchHomePageDelete.setVisibility(View.VISIBLE);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        initData();
        setListener();
        initListener();
        initAdapter();
        getDrawerData();
    }

    private void initListener() {
        searchHomePageCancel.setOnClickListener(this);

        goodsSortBzDrawerFabuStart.setOnClickListener(this);
        goodsSortBzDrawerFabuEnd.setOnClickListener(this);
        goodsSortBzDrawerShishiStart.setOnClickListener(this);
        goodsSortBzDrawerShishiEnd.setOnClickListener(this);

        goodsSortBzDrawerReset.setOnClickListener(this);
        goodsSortBzDrawerSure.setOnClickListener(this);


        goodsSortTsDrawerFenleiTv.setOnClickListener(this);
        goodsSortTsDrawerChubansheTv.setOnClickListener(this);
        goodsSortTsDrawerChubantimeStart.setOnClickListener(this);
        goodsSortTsDrawerChubantimeEnd.setOnClickListener(this);

        goodsSortTsDrawerReset.setOnClickListener(this);
        goodsSortTsDrawerSure.setOnClickListener(this);
        //设置抽屉不可手势划出
        searchResultDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        search_result_EditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SearchResaultActivity.this, SearchHomePageActivity.class));
                finish();
            }
        });

        searchHomePageDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search_result_EditText.setText("");
//                popupWindow.dismiss();
            }
        });
    }
    private void initData() {
        titleList.add("全部");
        titleList.add("标准");
        titleList.add("培训");
        titleList.add("商品");
        titleList.add("文章");
        titleList.add("问答");
//        fragmentList.add(new SearchQuanBuFragment());
        fragmentList.add(SearchAllFragment.newInstance(SearchResaultActivity.this, nowSearchStr));
        fragmentList.add(new SearchBiaoZhunFragment());
        fragmentList.add(new SearchPeiXunFragment());
        fragmentList.add(new SearchGoodsFragment());
        fragmentList.add(new SearchArticleFragment());
        fragmentList.add(new SearchWenDaFragment());
        vpAdapter = new SearchResultVpAdapter(getSupportFragmentManager(), titleList, fragmentList);


        searchVp.setAdapter(vpAdapter);
    }

    protected void setListener() {
        searchTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getText().equals("全部") || tab.getText().equals("文章") || tab.getText().equals("培训")||tab.getText().equals("问答")) {
                    searchShaixuanTv.setVisibility(View.GONE);
                    searchResultShaiXuanImg.setVisibility(View.GONE);
                } else {
                    searchShaixuanTv.setVisibility(View.VISIBLE);
                    searchResultShaiXuanImg.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        searchShaixuanTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchTabLayout.getSelectedTabPosition() == 1) {
                    openBzDrawer();
                } else if (searchTabLayout.getSelectedTabPosition() == 3) {
                    openGoodsDrawer();
                }
            }
        });
        searchResultShaiXuanImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchTabLayout.getSelectedTabPosition() == 1) {
                    openBzDrawer();
                } else if (searchTabLayout.getSelectedTabPosition() == 3) {
                    openGoodsDrawer();
                }
            }
        });
    }

    //获取图书属性数据监听
    private void getDrawerData() {
        SearchTsAttributeApi goodsSortApi = new SearchTsAttributeApi(this, delCartListener);
        goodsSortApi.setCache(false);
        HttpManager.getInstance().doHttpDeal(goodsSortApi);
    }

    //获取图书属性数据监听
    HttpOnNextListener delCartListener = new HttpOnNextListener<BaseResultEntity>() {
        @Override
        public void onNext(BaseResultEntity subjects) {
            if (!subjects.getCode().equals("200")) {
                Log.e("cjx", "图书属性:" + subjects.getData());
                return;
            }
            SearchTsAttributeBean bean = (SearchTsAttributeBean) subjects.getData();
            Log.e("cjx", "图书属性拿到数据");
            tsTsFenLeiDrawerList.addAll(bean.getBookCat());
            int attrId = bean.getAllAttrList().get(0).getAttrId();
            tsDrawerFenLeiRvAdapter.setTsTsFenLeiDrawerList(tsTsFenLeiDrawerList);
            List<SearchTsAttributeBean.AllAttrListBean> allAttrDrawerList = bean.getAllAttrList();
            List<String> attrValuesList = allAttrDrawerList.get(0).getAttrValues();
            for (int i = 0; i < attrValuesList.size(); i++) {
                String s = attrValuesList.get(i);
                GoodsSortDrawerBean goodsSortDrawerBean = new GoodsSortDrawerBean(s, attrId + "", false, true);
                chubansheDrawerList.add(goodsSortDrawerBean);
            }
            tsDrawerChuBanSheRvAdapter.setTsTsFenLeiDrawerList(chubansheDrawerList);
            GoodsSortDrawerBean timeBean1 = new GoodsSortDrawerBean("近一个月", 95 + "", false, true);
            GoodsSortDrawerBean timeBean2 = new GoodsSortDrawerBean("近三个月", 95 + "", false, true);
            GoodsSortDrawerBean timeBean3 = new GoodsSortDrawerBean("近半年", 95 + "", false, true);
            GoodsSortDrawerBean timeBean4 = new GoodsSortDrawerBean("近一年", 95 + "", false, true);
            timeDrawerList.add(timeBean1);
            timeDrawerList.add(timeBean2);
            timeDrawerList.add(timeBean3);
            timeDrawerList.add(timeBean4);
            tsDrawerDateRvAdapter.setTsDrawerDateList(timeDrawerList);
        }

        @Override
        public void onCacheNext(String cache) {
        }

        @Override
        public void onError(final Throwable e) {
            Log.e("cjx", "bz拿到数据" + e.getMessage());
        }
    };

    private void initAdapter() {
        GoodsSortDrawerBean timeBean1 = new GoodsSortDrawerBean("近一个月", "60", false, true);
        GoodsSortDrawerBean timeBean2 = new GoodsSortDrawerBean("近三个月", "60", false, true);
        GoodsSortDrawerBean timeBean3 = new GoodsSortDrawerBean("近半年", "60", false, true);
        GoodsSortDrawerBean timeBean4 = new GoodsSortDrawerBean("近一年", "60", false, true);

        timeList.add(timeBean1);
        timeList.add(timeBean2);
        timeList.add(timeBean3);
        timeList.add(timeBean4);

        GoodsSortDrawerBean timeBean5 = new GoodsSortDrawerBean("近一个月", "61", false, true);
        GoodsSortDrawerBean timeBean6 = new GoodsSortDrawerBean("近三个月", "61", false, true);
        GoodsSortDrawerBean timeBean7 = new GoodsSortDrawerBean("近半年", "61", false, true);
        GoodsSortDrawerBean timeBean8 = new GoodsSortDrawerBean("近一年", "61", false, true);

        timeList1.add(timeBean5);
        timeList1.add(timeBean6);
        timeList1.add(timeBean7);
        timeList1.add(timeBean8);

        GoodsSortDrawerBean stateBean1 = new GoodsSortDrawerBean("有效", "53", false, true);
        GoodsSortDrawerBean stateBean2 = new GoodsSortDrawerBean("废止", "53", false, true);
        GoodsSortDrawerBean stateBean3 = new GoodsSortDrawerBean("限制", "53", false, true);

        stateList.add(stateBean1);
        stateList.add(stateBean2);
        stateList.add(stateBean3);

        drawerUptimeAdapter = new GoodsSortBzDrawerUpTimeRvAdapter(this, timeList);
        drawerUptimeAdapter.setGoodsSortBzDrawerUpTimeListener(this);
        drawerStateAdapter = new GoodsSortBzDrawerStateRvAdapter(this, stateList);
        drawerStateAdapter.setGoodsSortBzDrawerStateListener(this);
        drawerDowntimeAdapter = new GoodsSortBzDrawerDownTimeRvAdapter(this, timeList1);
        drawerDowntimeAdapter.setGoodsSortBzDrawerDownTimeListener(this);
        //标准状态
        GridLayoutManager gridLayoutManager2 = new GridLayoutManager(this, 3);
        goodsSortBzDrawerTopRv.setLayoutManager(gridLayoutManager2);
        goodsSortBzDrawerTopRv.setAdapter(drawerStateAdapter);
        //发布日期
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        goodsSortBzDrawerFabuRv.setLayoutManager(gridLayoutManager);
        goodsSortBzDrawerFabuRv.setAdapter(drawerUptimeAdapter);
        //实施日期
        GridLayoutManager gridLayoutManager1 = new GridLayoutManager(this, 3);
        goodsSortBzDrawerShishiRv.setLayoutManager(gridLayoutManager1);
        goodsSortBzDrawerShishiRv.setAdapter(drawerDowntimeAdapter);

        GridLayoutManager tsFenLeiManager = new GridLayoutManager(this, 3);
        goodsSortTsDrawerFenleiRv.setLayoutManager(tsFenLeiManager);
        tsDrawerFenLeiRvAdapter = new GoodsSortTsDrawerFenLeiRvAdapter(this);
        goodsSortTsDrawerFenleiRv.setAdapter(tsDrawerFenLeiRvAdapter);
        tsDrawerFenLeiRvAdapter.setGoodsSortBzDrawerFenLeiListener(this);

        GridLayoutManager tsChuBanSheManager = new GridLayoutManager(this, 2);
        goodsSortTsDrawerChubansheRv.setLayoutManager(tsChuBanSheManager);
        tsDrawerChuBanSheRvAdapter = new GoodsSortTsDrawerChuBanSheRvAdapter(this);
        goodsSortTsDrawerChubansheRv.setAdapter(tsDrawerChuBanSheRvAdapter);
        tsDrawerChuBanSheRvAdapter.setGoodsSortBzDrawerStateListener(this);

        GridLayoutManager tsDateManager = new GridLayoutManager(this, 3);
        goodsSortTsDrawerChubantimeRv.setLayoutManager(tsDateManager);
        tsDrawerDateRvAdapter = new GoodsSortTsDrawerDateRvAdapter(this);
        goodsSortTsDrawerChubantimeRv.setAdapter(tsDrawerDateRvAdapter);
        tsDrawerDateRvAdapter.setGoodsSortBzDrawerDateListener(this);

//        searchHomePageRecyclerView.setLayoutManager(new LinearLayoutManager(SearchHomePageActivity.this));
//        buQuanRvAdapter = new SearchHomePageBuQuanRvAdapter(SearchHomePageActivity.this);
//        buQuanRvAdapter.setSearchHomePageRvListener(SearchHomePageActivity.this);
//        searchHomePageRecyclerView.setAdapter(buQuanRvAdapter);

    }

    //打开标准抽屉
    public void openBzDrawer() {
        linearTs.setVisibility(View.VISIBLE);
        linearGoods.setVisibility(View.GONE);
        searchResultDrawerLayout.openDrawer(GravityCompat.END);
    }

    //打开商品抽屉
    public void openGoodsDrawer() {
        linearTs.setVisibility(View.GONE);
        linearGoods.setVisibility(View.VISIBLE);
        searchResultDrawerLayout.openDrawer(GravityCompat.END);
    }

    //时间选择弹窗
    private void commonPop(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortBzDrawerFabuStart.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    @Override
    protected int getContentViewId() {
        return 0;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void addListener() {

    }

    //时间选择弹窗
    private void commonPop1(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortBzDrawerFabuEnd.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //时间选择弹窗
    private void commonPop2(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortBzDrawerShishiStart.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //时间选择弹窗
    private void commonPop3(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortBzDrawerShishiEnd.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //时间选择弹窗
    private void commonPop4(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortTsDrawerChubantimeStart.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    //时间选择弹窗
    private void commonPop5(View view) {
        PopupWindow popupWindow = new PopupWindow(view, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        //从下往上弹出动画效果
        popupWindow.setAnimationStyle(R.style.bottomPopupAnimation);
        popupWindow.showAtLocation(findViewById(R.id.goods_sort_bz_drawer), Gravity.BOTTOM, 0, 0);
        DatePicker picker = view.findViewById(R.id.datePicker_picker);
        picker.init(2019, 8, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Log.e("cjx", year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
                goodsSortTsDrawerChubantimeEnd.setText(year + "年" + (monthOfYear + 1) + "月" + dayOfMonth + "日");
            }
        });
    }

    @Override
    public void onUpTimeClick(int index) {
        for (int i = 0; i < timeList.size(); i++) {
            if (i == index)
                timeList.get(i).setFlag(!timeList.get(i).isFlag());
            else
                timeList.get(i).setFlag(false);
        }
        if (timeList.get(index).isFlag()) {
            goodsSortBzDrawerFabuStart.setClickable(false);
            goodsSortBzDrawerFabuEnd.setClickable(false);
        } else {
            goodsSortBzDrawerFabuStart.setClickable(true);
            goodsSortBzDrawerFabuEnd.setClickable(true);
        }
        drawerUptimeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onStateClick(int index) {
        for (int i = 0; i < stateList.size(); i++) {
            if (i == index) {
                stateList.get(i).setFlag(!stateList.get(i).isFlag());
            } else {
                stateList.get(i).setFlag(false);
            }
        }
        goodsSortBzDrawerZhankai.setText(stateList.get(index).getS() + "  ");
        drawerStateAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDownTime(int index) {
        for (int i = 0; i < timeList1.size(); i++) {
            if (i == index)
                timeList1.get(i).setFlag(!timeList1.get(i).isFlag());
            else
                timeList1.get(i).setFlag(false);
        }
        if (timeList1.get(index).isFlag()) {
            goodsSortBzDrawerShishiStart.setClickable(false);
            goodsSortBzDrawerShishiEnd.setClickable(false);
        } else {
            goodsSortBzDrawerShishiStart.setClickable(true);
            goodsSortBzDrawerShishiEnd.setClickable(true);
        }
        drawerDowntimeAdapter.notifyDataSetChanged();
    }

    @Override
    public void onTsFenLeiSelected(int index) {
        for (int i = 0; i < tsTsFenLeiDrawerList.size(); i++) {
            if (i == index) {
                tsTsFenLeiDrawerList.get(i).setSelected(!tsTsFenLeiDrawerList.get(i).isSelected());
            } else {
                tsTsFenLeiDrawerList.get(i).setSelected(false);
            }
        }
        goodsSortTsDrawerFenleiTv.setText(tsTsFenLeiDrawerList.get(index).getCatName() + "  ");
        tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
    }

    @Override
    public void onTsDrawerChuBanSheSelected(int index) {
        for (int i = 0; i < chubansheDrawerList.size(); i++) {
            chubansheDrawerList.get(index).setFlag(!chubansheDrawerList.get(i).isFlag());
        }
        StringBuffer sb = new StringBuffer();
        for (int j = 0; j < chubansheDrawerList.size(); j++) {
            if (chubansheDrawerList.get(j).isFlag()) {
                sb.append(chubansheDrawerList.get(j).getS() + "丶");
            }
        }
        goodsSortTsDrawerChubansheTv.setText(sb.toString());
        tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
    }

    @Override
    public void onTsDrawerDateSelected(int index) {
        for (int i = 0; i < timeDrawerList.size(); i++) {
            if (i == index) {
                timeDrawerList.get(i).setFlag(!timeDrawerList.get(i).isFlag());
            } else {
                timeDrawerList.get(i).setFlag(false);
            }
        }
        tsDrawerDateRvAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            //发布start时间
            case R.id.goods_sort_bz_drawer_fabu_start:
                View view = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop(view);
                for (int i = 0; i < timeList.size(); i++) {
                    timeList.get(i).setClickable(false);
                }
                drawerUptimeAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_bz_drawer_fabu_end:
                View view1 = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop1(view1);
                for (int i = 0; i < timeList.size(); i++) {
                    timeList.get(i).setClickable(false);
                }
                drawerUptimeAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_bz_drawer_shishi_start:
                View view2 = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop2(view2);
                for (int i = 0; i < timeList1.size(); i++) {
                    timeList1.get(i).setClickable(false);
                }
                drawerDowntimeAdapter.notifyDataSetChanged();
                break;
            case R.id.goods_sort_bz_drawer_shishi_end:
                View view3 = LayoutInflater.from(this).inflate(R.layout.view_datepicker, null);
                commonPop3(view3);
                for (int i = 0; i < timeList1.size(); i++) {
                    timeList1.get(i).setClickable(false);
                }
                drawerDowntimeAdapter.notifyDataSetChanged();
                break;
            //清空重置
            case R.id.goods_sort_bz_drawer_Reset:
                goodsSortBzDrawerZhankai.setText("全部  ");
                for (int i = 0; i < stateList.size(); i++) {
                    stateList.get(i).setClickable(true);
                    stateList.get(i).setFlag(false);
                }
                goodsSortBzDrawerFabuStart.setText("起始日期");
                goodsSortBzDrawerFabuEnd.setText("截止日期");
                goodsSortBzDrawerShishiStart.setText("起始日期");
                goodsSortBzDrawerShishiEnd.setText("截止日期");
                for (int j = 0; j < timeList.size(); j++) {
                    timeList.get(j).setFlag(false);
                    timeList.get(j).setClickable(true);
                }
                drawerUptimeAdapter.notifyDataSetChanged();
                for (int k = 0; k < timeList1.size(); k++) {
                    timeList1.get(k).setFlag(false);
                    timeList1.get(k).setClickable(true);
                }
                drawerDowntimeAdapter.notifyDataSetChanged();
                goodsSortBzDrawerQicaoEdit.setText("");
                goodsSortBzDrawerDanweiEdit.setText("");
                drawerStateAdapter.notifyDataSetChanged();
                break;
            //Bz确认
            case R.id.goods_sort_bz_drawer_Sure:
                initBzSureData();
                searchResultDrawerLayout.closeDrawer(GravityCompat.END);
                break;
            case R.id.search_result_Cancel:
                this.finish();
                //关闭软键盘
                EditUtils.showORhideSoftKeyboard(SearchResaultActivity.this);
                break;
            case R.id.goods_sort_ts_drawer_Reset:
                goodsSortTsDrawerFenleiTv.setText("全部  ");
                goodsSortTsDrawerChubansheTv.setText("全部  ");
                goodsSortTsDrawerChubantimeStart.setText("起始日期");
                goodsSortTsDrawerChubantimeEnd.setText("截止日期");
                for (int i = 0; i < tsTsFenLeiDrawerList.size(); i++) {
                    tsTsFenLeiDrawerList.get(i).setSelected(false);
                }
                tsDrawerFenLeiRvAdapter.notifyDataSetChanged();
                for (int i = 0; i < chubansheDrawerList.size(); i++) {
                    chubansheDrawerList.get(i).setFlag(false);
                }
                tsDrawerChuBanSheRvAdapter.notifyDataSetChanged();
                for (int i = 0; i < timeDrawerList.size(); i++) {
                    timeDrawerList.get(i).setFlag(false);
                }
                tsDrawerDateRvAdapter.notifyDataSetChanged();
                break;
            //Ts确认
            case R.id.goods_sort_ts_drawer_Sure:
                initTsSureData();
                searchResultDrawerLayout.closeDrawer(GravityCompat.END);
                break;
        }
    }

    private void initBzSureData() {
        String bzNowAttrPro = getBzNowAttrPro();
        Log.e(TAG, "initBzSureData: "+bzNowAttrPro );
        EventBus.getDefault().post(new MessageEvent(bzNowAttrPro,"bzSure"));
    }

    //获取当前筛选选中attrPro
    private String getBzNowAttrPro() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < stateList.size(); i++) {
            if (stateList.get(i).isFlag()) {
                sb.append(stateList.get(i).getAttrId() + "__" + stateList.get(i).getS());
            }
        }
        if (goodsSortBzDrawerFabuStart.getText().toString().equals("起始日期") && goodsSortBzDrawerFabuEnd.getText().toString().equals("截止日期")) {
            for (int i = 0; i < timeList.size(); i++) {
                if (timeList.get(i).isFlag()) {
                    if (timeList.get(i).getS().equals("近一个月")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(30);
                        sb.append("&start60__" + beforeDateTime);
                    } else if (timeList.get(i).getS().equals("近三个月")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(90);
                        sb.append("&start60__" + beforeDateTime);
                    } else if (timeList.get(i).getS().equals("近半年")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(180);
                        sb.append("&start60__" + beforeDateTime);
                    } else if (timeList.get(i).getS().equals("近一年")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(365);
                        sb.append("&start60__" + beforeDateTime);
                    }
                    String nowDateTime = DateUtils.getNowDateTime();
                    sb.append("&end60__" + nowDateTime);
                }
            }

        } else {
            sb.append("&start60__" + goodsSortBzDrawerFabuStart.getText().toString());
            sb.append("&end60__" + goodsSortBzDrawerFabuEnd.getText().toString());
        }
        if (goodsSortBzDrawerShishiStart.getText().toString().equals("起始日期") && goodsSortBzDrawerShishiEnd.getText().toString().equals("截止日期")) {
            for (int i = 0; i < timeList1.size(); i++) {
                if (timeList1.get(i).isFlag()) {
                    if (timeList1.get(i).getS().equals("近一个月")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(30);
                        sb.append("&start61__" + beforeDateTime);
                    } else if (timeList1.get(i).getS().equals("近三个月")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(90);
                        sb.append("&start61__" + beforeDateTime);
                    } else if (timeList1.get(i).getS().equals("近半年")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(180);
                        sb.append("&start61__" + beforeDateTime);
                    } else if (timeList1.get(i).getS().equals("近一年")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(365);
                        sb.append("&start61__" + beforeDateTime);
                    }
                    String nowDateTime = DateUtils.getNowDateTime();
                    sb.append("&end61__" + nowDateTime);
                }
            }

        } else {
            sb.append("&start61__" + goodsSortBzDrawerFabuStart.getText().toString());
            sb.append("&end61__" + goodsSortBzDrawerFabuEnd.getText().toString());
        }
        String qicao = goodsSortBzDrawerQicaoEdit.getEditableText().toString();
        Log.e("cjx", "起草人：" + qicao);
        if (!qicao.equals("")) {
            sb.append("&52__" + qicao);
        }
        String danwei = goodsSortBzDrawerDanweiEdit.getEditableText().toString();
        if (!danwei.equals("")) {
            sb.append("&64__" + danwei);
        }
        Log.e("cjx", "拼接结果：" + sb.toString());
        return sb.toString();
    }

    private void initTsSureData() {
        String attrPro = getTsNowAttrPro();
        Log.e(TAG, "initTsSureData: "+attrPro );
        EventBus.getDefault().post(new MessageEvent(attrPro,"tsSure"));
    }

    private String getTsNowAttrPro() {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < tsTsFenLeiDrawerList.size(); i++) {
            if (tsTsFenLeiDrawerList.get(i).isSelected()) {
                sb.append(tsTsFenLeiDrawerList.get(i).getCatId() + "__" + tsTsFenLeiDrawerList.get(i).getCatName());
            }
        }
        for (int i = 0; i < chubansheDrawerList.size(); i++) {
            if (chubansheDrawerList.get(i).isFlag()) {
                sb.append("&95__" + chubansheDrawerList.get(i).getS());
            }
        }
        String author = goodsSortTsDrawerAuthorEdit.getEditableText().toString();
        if (!author.equals("")) {
            sb.append("&91__" + author);
        }
        if (goodsSortTsDrawerChubantimeStart.getText().toString().equals("起始日期") && goodsSortTsDrawerChubantimeEnd.getText().toString().equals("截止日期")) {
            for (int i = 0; i < timeDrawerList.size(); i++) {
                if (timeDrawerList.get(i).isFlag()) {
                    if (timeDrawerList.get(i).getS().equals("近一个月")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(30);
                        sb.append("&start93__" + beforeDateTime);
                    } else if (timeDrawerList.get(i).getS().equals("近三个月")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(90);
                        sb.append("&start93__" + beforeDateTime);
                    } else if (timeDrawerList.get(i).getS().equals("近半年")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(180);
                        sb.append("&start93__" + beforeDateTime);
                    } else if (timeDrawerList.get(i).getS().equals("近一年")) {
                        String beforeDateTime = DateUtils.getBeforeDateTime(365);
                        sb.append("&start93__" + beforeDateTime);
                    }
                    String nowDateTime = DateUtils.getNowDateTime();
                    sb.append("&end93__" + nowDateTime);
                }
            }
        } else {
            sb.append("&start93__" + goodsSortTsDrawerChubantimeStart.getText().toString());
            sb.append("&end93__" + goodsSortTsDrawerChubantimeEnd.getText().toString());
        }
        Log.e("cjx", "图书拼接属性：" + sb.toString());
        return sb.toString();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            this.finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

//    private void popupWinDowShowBuQuanRv() {
//        View view = LayoutInflater.from(this).inflate(R.layout.view_search_popupwindow_rv,null);
//        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.popupWindow_rv);
//        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
//        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        recyclerView.setLayoutManager(linearLayoutManager);
//        buQuanRvAdapter = new SearchHomePageBuQuanRvAdapter(this);
//        recyclerView.setAdapter(buQuanRvAdapter);
//        popupWindow = new PopupWindow(searchResultDrawerLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//        popupWindow.setContentView(view);
//        popupWindow.setFocusable(false);
//        popupWindow.showAsDropDown(searchResultRelativeLayout);
//    }

    //获取搜索补全数据
//    private void initBuQuanData(String s1) {
//        SearchHomePageBuQuanApi goodsSortApi = new SearchHomePageBuQuanApi(this, buQuanListener, s1);
//        goodsSortApi.setCache(false);
//        HttpManager.getInstance().doHttpDeal(goodsSortApi);
//    }
//
//    //获取搜索补全数据监听
//    HttpOnNextListener buQuanListener = new HttpOnNextListener<BaseResultEntity>() {
//        @Override
//        public void onNext(BaseResultEntity subjects) {
//            if (!subjects.getCode().equals("200")) {
//                Log.e("cjx", "图书属性:" + subjects.getData());
//                return;
//            }
//            SearchPupopBean bean = (SearchPupopBean) subjects.getData();
//            List<String> buquanList = bean.getRelateList();
//            buQuanRvAdapter.setBuquanList(buquanList);
//            buQuanRvAdapter.notifyDataSetChanged();
//        }
//
//        @Override
//        public void onCacheNext(String cache) {
//        }
//
//        @Override
//        public void onError(final Throwable e) {
//            Log.e("cjx", "bz拿到数据" + e.getMessage());
//        }
//    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void cha(MessageEvent msg){

        String message = msg.getMessage();
        for (int i = 0; i < message.length(); i++) {
            if (!Character.isDigit(message.charAt(i))) {
                continue;
            }
            int e = Integer.parseInt(msg.getMessage());
            if (i<0 || i>5){
                return;
            }else {
                searchVp.setCurrentItem(e);
            }
        }


    }


    public static Intent getIntent(Context context, String searchStr){
        Intent intent = new Intent(context, SearchResaultActivity.class);
        intent.putExtra(SearchHomePageActivity.NOW_SEARCHSTR,searchStr);
        return intent;

    }

}